import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Bt {

    static List<List<Integer>> resList=new ArrayList<>();
    public static void bdf(TreeNode root){
        if(root==null)return;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()){
            ArrayList<Integer> layerList = new ArrayList<>();
            int len = queue.size();
            while(len>0){
                TreeNode poll = queue.poll();
                layerList.add(poll.val);
                if(poll.left!=null)queue.offer(poll.left);
                if(poll.right!=null)queue.offer(poll.right);
                len--;
            }
            resList.add(layerList);
        }
    }

    public static void main(String[] args) {
        TreeNode a4 = new TreeNode(5);
        TreeNode a3 = new TreeNode(4);

        TreeNode a2 = new TreeNode(3);
        TreeNode a1 = new TreeNode(2, a3, a4);
        TreeNode root=new TreeNode(1,a1,a2);

        bdf(root);
        resList.forEach(System.out::println);

    }


}
class TreeNode{
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }

    public TreeNode(int val) {
        this.val = val;
    }

    public TreeNode() {
    }
}